<template>
  <div class="login-container">
    <img :src="bg" class="wave" alt="">
    <div class="container">
      <div class="img">
        <img :src="currentWeek" alt="">
      </div>
      <div class="login-box">
        <div class="login-form">
          <Icon svg :width="100" :height="100" :content="'icon-xiezuo'" class="avatar" />
          <h2>
            Common Admin
          </h2>
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            auto-complete="on"
            label-position="left"
          >
            <el-form-item prop="username">
              <!-- <span class="svg-container">
                <svg-icon icon-class="user" />
              </span> -->
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="Username"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </el-form-item>

            <el-form-item prop="password">
              <!-- <span class="svg-container">
                <svg-icon icon-class="password" />
              </span> -->
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="Password"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
              <!-- <span
                class="show-pwd"
                @click="showPwd"
              >
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span> -->
            </el-form-item>

            <el-button
              :loading="loading"
              type="primary"
              style="width:100%;margin-bottom:30px;"
              @click.native.prevent="handleLogin"
            >登录</el-button>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import illustration0 from '@/assets/login/illustration0.svg'
import illustration1 from '@/assets/login/illustration1.svg'
import illustration2 from '@/assets/login/illustration2.svg'
import illustration3 from '@/assets/login/illustration3.svg'
import illustration4 from '@/assets/login/illustration4.svg'
import illustration5 from '@/assets/login/illustration5.svg'
import illustration6 from '@/assets/login/illustration6.svg'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {},
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      bg: require('@/assets/login/bg.png'),
      avatar: require('@/assets/login/avatar.svg')
    }
  },
  computed: {
    currentWeek() {
      switch (String(new Date().getDay())) {
        case '0':
          return illustration0
        case '1':
          return illustration1
        case '2':
          return illustration2
        case '3':
          return illustration3
        case '4':
          return illustration4
        case '5':
          return illustration5
        case '6':
          return illustration6
        default:
          return illustration4
      }
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import url('../../styles/login.scss');
</style>
